clear;clc;
%% create session, connect devices
session=MeasurementSession.SimpleMeasurement([Constant.AutoMeasurementRootPath '/Application/ColdCellCalibration/device.dat']);
% session.getAllDevicesInBase('session');

%% Device Settings

% sweeper
zi = session.getDevice('dev3543');
sweeper = zi.sweepModule();
sweeper.setControl('xmapping', 0, 'start', 0, 'stop', 1, 'gridnode', 'sigouts/0/offset')...
       .setFilter('bandwidthcontrol', 0)...
       .setSettling('settling_tc', 1)...
       .setStatistics('averaging_sample', 100)...
       .setOption()...
       .showParam();

zi.setSigOutImpedance50().enableSigOutAutoRange.fireSigOut();
   
sweeper.subscribeChannel('0');
result = sweeper.run('processMonitor', @(data, idx) monitorFunc(data, idx));
sweeper.unsubscribeChannel('0');

ColdCellPowerCalibrationData = PowerCalibration( result.dev3543.demods.sample{1} );
ColdCellPowerCalibrationData.setStandardIncPower( input( 'Input Actual Incident Power in mW, P_inc = ' ) );


%% Save to File
save( sprintf('%s\\Application\\ColdCellCalibration\\%s', ...
              Constant.AutoMeasurementRootPath, ColdCellPowerCalibrationData.filename),...
      'ColdCellPowerCalibrationData');

%% Monitor During Data Acquisition
function monitorFunc(data, ~)
    subplot(2, 1, 1);
    plot(data.grid, data.auxin0, 'r.-', data.grid, data.auxin1, 'b.-');
    xlabel('AOM Voltage (V)'); ylabel('PD1/2 Signal (V)');
    legend({'PD1', 'PD2'}, 'Location','northwest');
    subplot(2, 1, 2);
    plot( data.auxin0, data.auxin1, '.-' );
    xlabel('PD1 Signal (V)'); ylabel('PD2 Signal (V)');
end

